function updateTips(tips, message) {
    tips.text(message).addClass("ui-state-highlight");
    
    setTimeout(function() {
      tips.removeClass("ui-state-highlight", 1500);
    }, 500);
  }

function checkLength(object, name, min, max, tips) {
	if (object.val().length > max || object.val().length < min) {
		object.addClass("ui-state-error");
		updateTips(tips, "Length of " + name + " must be between " + min
				+ " and " + max + ".");
		return false;
	} else {
		return true;
	}
}

function checkHours(object, name, maxHours, tips) {
	if (object.val() > maxHours) {
		object.addClass("ui-state-error");
		updateTips(tips, name + " must be be less than or equal to " + maxHours + " hours.");
		return false;
	} else {
		return true;
	}
}

function checkEmail(source, id, emailTextBox, name, tips) {
	var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var returnStatus = false;

	// check for unique email address in database

	// get team member data by email
	$.ajax({
		url: "GetTeamMemberData",
		dataType: 'json',
		async: false,
		data: { teamMemberId: id, teamMemberEmail: emailTextBox.val() },
		success: function(result) {
			var emailFound = false;
			  
			$.each(result, function(i, row) {
				if (result.email != null) {
					emailFound = true;
				}

				if (source == "ADD") {
					if (emailFound) {
						emailTextBox.addClass("ui-state-error");
						updateTips(tips, "Email address " + emailTextBox.val() + " already exists for " + result.name + ".");
						returnStatus = false;
						return false;
					} else {
						returnStatus = true;
						return true;
					}
				}
				else {	// EDIT
					if (emailFound) {
						if (result.id == id) {
							returnStatus = true;
							return true;	// email has not changed
						} else {
							// email address belongs to another team member
							emailTextBox.addClass("ui-state-error");
							updateTips(tips, "Email address " + emailTextBox.val() + " already belongs to " + result.name + ".");
							returnStatus = false;
							return false;
						}
					} else {
							returnStatus = true;
							return true;	// email address was not found
					}
				}
			});
		}
	}).fail(function() {
		alert('Error getting JSON for team member');
	});

	if (returnStatus == false) {
		return false;
	}
	
	if (regex.test(emailTextBox.val())) {
		return true;
	} else {
		emailTextBox.addClass("ui-state-error");
		updateTips(tips, name + " must be a valid email address.");
		return false;
	}
}

// only one product owner can exist for a project
function validateProductOwnerExists(teamMemberId, role, project, tips) {
	var productOwner = null;
	var foundProductOwner = false;
	
	// get product owner for given project id
	$.ajax({
		  url: "GetProductOwnerForProject",
		  dataType: 'json',
		  async: false,
		  data: { projectId: project.val() },
		  success: function(result) {
			  $.each(result, function(i, row) {
				  productOwner = result;
			  });
			
			  if (productOwner.name == null) {
				  foundProductOwner = true;
				  return true;
			  }
			  else {
				  if (teamMemberId == productOwner.id) {
					  // Team member is the product owner for this project, so we are ok
					  foundProductOwner = true;
					  return true;
				  }

				  role.addClass("ui-state-error");
				  updateTips(tips, "Only one Product Owner is allowed per project. "
						  + productOwner.name + " is currently the product owner for this project.");
				  return false;
			  }
		  }
		}).fail(function() {
			alert('Error getting JSON for project product owner');
		});

	return foundProductOwner;
}

function validateProjectNameDoesNotExist(name, project_tips) {
	var project = null;
	var foundProject = false;
	
	// find project name
	$.ajax({
		url: "GetProjectByName",
		dataType: 'json',
		async: false,
		data: { project_name: name.val() },
	}).done(function(result) {
		  $.each(result, function(i, row) {
			  project = result;
		  });

		  if (project.name == null) {
			  return true;
		  }
		  else {
			  name.addClass("ui-state-error");
			  updateTips(project_tips, "Project name must be unique.");
			  foundProject = true;
			  return false;
		  }
	}).fail(function() {
		alert('Error getting JSON for project name');
	});
	
	return ! foundProject;
}

function insertRowSortedByName(row, id, insertDataInList, appendDataToList) {

	// find row to insert
	var inserted = false;
	
	$('#' + id + ' tr').not('thead tr').each(function(index) {
		var tableRowName = $(this).find(".name-column").html();
		if (row.name.toLowerCase() < tableRowName.toLowerCase()) {
			insertDataInList(this, row);
			inserted = true;
			return false;
		}
	});

	if (! inserted) {
		appendDataToList(row);
	}
}

function checkMatchingPasswords(password, password2, team_member_tips) {
	if (password.val() !== password2.val()) {
		password.addClass("ui-state-error");
		updateTips(team_member_tips, "Passwords must match.");
		return false;
	} else {
		return true;
	}
}

function setProjectIdAndName(project_id) {
	var parameters = "projectId=" + project_id;
	$.post("SetCurrentProject", parameters).done(function(data) {
		var returnedData = jQuery.parseJSON(data);
		sessionProjectId = project_id;
		$('#projectName').html(returnedData.project_name);
	}).fail(function() {
		alert('Error setting project information for session');
	});
}
